package cn.breeze.system.controller;

import cn.breeze.basic.annotation.PreAuthorize;
import cn.breeze.system.domain.Menu;
import cn.breeze.system.service.IRoleService;
import cn.breeze.system.domain.Role;
import cn.breeze.system.query.RoleQuery;
import cn.breeze.basic.utils.AjaxResult;
import cn.breeze.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.breeze.basic.exception.BusinessException;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private  IRoleService roleService;


    /**
     * 保存和修改公用的
     * @param role  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @PreAuthorize(name = "保存角色", sn = "role:save")
    public AjaxResult addOrUpdate(@RequestBody Role role){
        try {
            if( role.getId()!=null)
                roleService.update(role);
            else
                roleService.save(role);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @PreAuthorize(name = "删除角色", sn = "role:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            roleService.remove(id);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试1");
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    @PreAuthorize(name = "获取角色", sn = "role:get")
    public Role get(@PathVariable("id")Long id)
    {
        return roleService.loadById(id);
    }

    /**
    * @Title: getByEmpId
    * @Description: 根据employee的主键id查询所有role
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/7/15 1:16
    * @Parameters: [id]
    * @Return cn.breeze.system.domain.Role
    */
    @GetMapping("/roleEcho/{id}")
    /*@PreAuthorize(name = "获取角色", sn = "role:get")*/
    public List<Long> getByEmpId(@PathVariable("id")Long id)
    {
        return roleService.loadByEmpId(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    @PreAuthorize(name = "获取所有角色", sn = "role:loadAll")
    public List<Role> list(){

        return roleService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    @PreAuthorize(name = "列表角色", sn = "role:list")
    public PageList<Role> queryData(@RequestBody RoleQuery query)
    {
        return roleService.queryData(query);
    }

    /**
    * @Title: ownMenus
    * @Description: 所有所有菜单
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/7/13 23:50
    * @Parameters: [id]
    * @Return java.util.List<cn.breeze.system.domain.Menu>
    */
    @GetMapping(value="/ownMenus/{id}")
    @PreAuthorize(name = "所有所有菜单", sn = "role:ownMenus")
    public List<Menu> ownMenus(@PathVariable("id") Long id){
        List<Menu> menus = roleService.ownMenus(id);
        return menus;
    }


    @GetMapping(value="/permissions/{id}")
    @PreAuthorize(name = "所有所有菜单", sn = "role:ownMenus")
    public Long[] permissions(@PathVariable("id") Long id){
        Long[] permissions = roleService.loaadPermissions(id);
        return permissions;
    }
}
